package com.example.bingobox.ui.todobox;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.bingobox.R;
import com.example.bingobox.model.TodoBox;
import java.util.ArrayList;
import java.util.List;

public class TodoBoxSelectionAdapter extends RecyclerView.Adapter<TodoBoxSelectionAdapter.ViewHolder> {
    private final List<TodoBox> todoBoxes;
    private final List<TodoBox> selectedBoxes = new ArrayList<>();

    public TodoBoxSelectionAdapter(List<TodoBox> todoBoxes) {
        this.todoBoxes = todoBoxes;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_todobox_selection, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        TodoBox todoBox = todoBoxes.get(position);
        holder.checkBox.setChecked(selectedBoxes.contains(todoBox));
        holder.nameText.setText(todoBox.getName());

        View.OnClickListener clickListener = v -> {
            boolean isChecked = !holder.checkBox.isChecked();
            holder.checkBox.setChecked(isChecked);
            if (isChecked) {
                selectedBoxes.add(todoBox);
            } else {
                selectedBoxes.remove(todoBox);
            }
        };

        holder.itemView.setOnClickListener(clickListener);
        holder.checkBox.setOnClickListener(clickListener);
    }

    @Override
    public int getItemCount() {
        return todoBoxes.size();
    }

    public List<TodoBox> getSelectedBoxes() {
        return new ArrayList<>(selectedBoxes);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        CheckBox checkBox;
        TextView nameText;

        ViewHolder(View itemView) {
            super(itemView);
            checkBox = itemView.findViewById(R.id.checkbox_todobox);
            nameText = itemView.findViewById(R.id.text_todobox_name);
        }
    }
} 